package com.haierp.controller.yitao;

import com.haierp.service.IOuterOrderService;
import com.haierp.service.yitao.IYtOrderService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/yitaoPushMessage")
public class YiTaoPushMessageController {

    @Autowired
    private IYtOrderService iYtOrderService;

    @Autowired
    private IOuterOrderService outerOrderService;

    @RequestMapping(value = "/pullOrder", method = RequestMethod.POST)
    @ResponseBody
    public Object pullOrder(HttpServletRequest request) {
        JSONObject res = new JSONObject();
        res.put("code", 0);
        res.put("msg", "success");

        List<Long> outOrderIdList = iYtOrderService.yitaoPushOrder(request);
        if (outOrderIdList.size() > 0) {
            //生成子订单并配货
            outOrderIdList.forEach(erpOrderId -> {
                outerOrderService.review(erpOrderId, "YT");
            });
        }//添加子订单
        return res;
    }

    @RequestMapping(value = "/pushOrder", method = RequestMethod.POST)
    @ResponseBody
    public Map pushOrder(HttpServletRequest request) {
        return iYtOrderService.yitaoPullOrder(request);
    }

    @RequestMapping(value = "/pushOrderDetails", method = RequestMethod.POST)
    @ResponseBody
    public Object pushOrderDetails(HttpServletRequest request) {
        return iYtOrderService.pushOrderDetails(request);
    }
}
